package com.freesun.shop.service.imp;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.freesun.shop.entity.HotSearch;
import com.freesun.shop.mapper.HotSearchMapper;
import com.freesun.shop.service.HotSearchService;
import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;

import java.util.List;

/**
 * <p>
 * 热搜 服务实现类
 * </p>
 *
 * @author zf
 * @since 2019-11-13
 */
@Service
@CacheConfig(cacheNames = "com.freesun.shop.entity.HotSearch")
public class HotSearchServiceImpl extends ServiceImpl<HotSearchMapper, HotSearch> implements HotSearchService {

    private static Logger logger = LoggerFactory.getLogger(HotSearchServiceImpl.class);


    private static final String  HOT_SEARCH_TOPN_PREFIX = "'top-n-search-data'";
    @Override
    @Cacheable(key = HOT_SEARCH_TOPN_PREFIX)
    public List<HotSearch> topN(Integer number, Integer shopId, Integer sort) {
        logger.info("查询的商铺:{}的前:{}热搜数据",shopId,number);
        List<HotSearch> hotSearches = this.baseMapper.selectList(new LambdaQueryWrapper<HotSearch>()
                .eq(HotSearch::getShopId, shopId)
                .eq(HotSearch::getStatus, 1)
                .orderByAsc(HotSearch::getSeq)
                .last("limit " + number));
        return hotSearches;
    }
}
